<?php
/**
 * Created by PhpStorm.
 * User: LAKECASEY
 * Date: 2018/12/13
 * Time: 17:31
 */
namespace App\Http\Controllers\Web;
use App\Http\Controllers\Controller;
use App\model\Adverts;
use App\model\China;
use App\model\Information;
use Illuminate\Http\Request;

class NewsController extends Controller
{
    /**
     * 首页
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|\think\response\View
     */
    public function index(Request $request)
    {
        //SEO
        $SEO = config('seo.news');
        $title = $SEO['title'];
        $keywords = $SEO['keywords'];
        $description = $SEO['description'];
        $active = 3;
        //查询所有资讯
        $db = new Information();
        $getdefaultdata = $db->getdefaultnews($request->id);
        //查询广告位
        $ad= new Adverts();
        $news_ad_one = $ad->getad(2);
        $news_ad_two = $ad->getad(3);
        $news_ad_three = $ad->getad(4);
        $select_status =  $request->id;
        $adarray = [];
        if (!empty($news_ad_one) || !empty($news_ad_two) || !empty($news_ad_three))
        {
            $adarray = [
                'one' => isset($news_ad_one[0]['pictures']) ? $news_ad_one[0]['pictures'] : '',
                'oneurls' => isset($news_ad_one[0]['urls']) ? $news_ad_one[0]['urls'] : '',
                'onetitles' => isset($news_ad_one[0]['titles']) ? $news_ad_one[0]['titles'] : '',
                'two' => isset($news_ad_two[0]['pictures']) ? $news_ad_two[0]['pictures'] :'' ,
                'twourls' => isset($news_ad_two[0]['urls']) ? $news_ad_two[0]['urls'] :'',
                'twotitles' => isset($news_ad_two[0]['titles']) ? $news_ad_two[0]['titles'] : '',
                'three' => isset($news_ad_three[0]['pictures']) ? $news_ad_three[0]['pictures'] :'',
                'threeurls' => isset($news_ad_three[0]['urls']) ? $news_ad_three[0]['urls'] :'',
                'threetitles' => isset($news_ad_three[0]['titles']) ? $news_ad_three[0]['titles'] : '',
            ];
        }
        //热门
        $hot = $db->getHot();
        //判断是否是移动端
        $isMobile = $this->isMobile();
        if ($isMobile)
        {
            $active = '3-1';
            return view('wap.news.index',compact('title','keywords','description','active','getdefaultdata','select_status','hot'));
        }
        else {
            return view('web.news.index',compact('title','keywords','description','active','getdefaultdata','adarray','select_status','hot'));
        }

    }

    /**
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|\think\response\View
     */
    public function detials(Request $request)
    {
        //获取id
        $id = $request->id;
        //查询
        $db=new Information();
        $infos = $db->getDetials($id);
        $active = 3;
        //seo
        $SEO = config('seo.newsdetials');
        $title = sprintf($SEO['title'],$infos->titles);
        $keywords = sprintf($SEO['keywords'],$infos->titles);
        $description = sprintf($SEO['description'],$infos->contents);
        //其他新闻
        $othermsg = $db->getRand();
        //获取广告位
        $ad= new Adverts();
        $newsad= $ad->getad(5);
        //判断是否是移动端
        $isMobile = $this->isMobile();
        if ($isMobile)
        {
            $active = '3-2';
            return view('wap.news.detials',compact('title','keywords','description','active','infos','othermsg','newsad'));
        }
        else
        {
            return view('web.news.detials',compact('title','keywords','description','active','infos','othermsg','newsad'));
        }

    }

    /**
     * 获取推荐资讯
     * @param Request $request
     * @return string
     */
    public function forntinfos(Request $request)
    {
        if (!empty($request))
        {
            //获取表示1pc2手机
            $servertype = isset($request['server_type']) ? $request['server_type'] :1;
            $newdb=new Information();
            $suggest = $newdb->getSuggest($request['newstype']);
            if ($servertype == 2)
            {
                $html = "";
                foreach ($suggest as $s)
                {
                    $html.="<li> <a class=\"ov\" href=\"/news/$s->id.html\">· $s->titles</a></li>";
                }
                return $html;
            }
        }
    }
}